import { defineStore } from 'pinia'
import { ref } from 'vue'

// 定义 Store
export const useWorktoolStore = defineStore(
  'worktools',
  () => {
    const opts = ref({})
    const setOpts = (value) => {
      opts.value = value
    }
    //自选量具
    const notBluedev = ref(false)
    // 服务器端工具数据
    const tools = ref([])
    //移动端搜索到的设备
    const devices = ref([])
    const setDevices = (value) => {
      if (
        !devices.value
          .map((v) => {
            return v.deviceId
          })
          .includes(value.deviceId)
      )
        devices.value.push({ ...value })
    }

    const clearDevices = () => {
      devices.value = []
    }
    //
    //当前使用的设备
    const deviceId = ref({
      currDeviceId: '',
    })
    //清空当前使用设备
    const clearDeviceId = () => {
      deviceId.value = {
        currDeviceId: '',
      }
      notBluedev.value = false
    }

    // 设置所有tools数据
    const setTools = (value) => {
      tools.value = value
    }

    const clearTools = () => {
      tools.value = []
    }
    const setDeviceId = (value) => {
      deviceId.value = value
    }

    return {
      tools,
      devices,
      deviceId,
      notBluedev,
      opts,
      setTools,
      setDevices,
      clearTools,
      clearDevices,
      clearDeviceId,
      setOpts,
      setDeviceId,
    }
  },
  {
    // 网页端配置
    // persist: true,
    // 小程序端配置
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, value) {
          return uni.setStorageSync(key, value)
        },
      },
    },
  },
)
